package com.zzyl.web.controller.common;

import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.file.FileUtils;
import com.zzyl.oss.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

public class uploadFile {



    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            // String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            // String fileName = FileUploadUtils.upload(filePath, file);
            // String url = serverConfig.getUrl() + fileName;
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", url);
            ajax.put("newFileName", FileUtils.getName(url));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }



}
